package sk.tuke.client.view;

import sk.tuke.client.ApplicationClickHandler;
import sk.tuke.client.components.buttons.CrossMainViewButton;
import sk.tuke.client.components.forms.DataSourcePasswordField;
import sk.tuke.client.components.forms.DataSourceTextField;
import sk.tuke.client.interfaces.InitInterface;
import sk.tuke.client.interfaces.LayoutFactory;

import com.google.gwt.user.client.Timer;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.events.ErrorEvent;
import com.smartgwt.client.data.events.HandleErrorHandler;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginView extends HLayout implements InitInterface {

	boolean isWaitingForResponse;
	ClickEvent clickEvent;

	Timer timer;

	public LoginView() {
		mainLayout.setMembers(loginForm());
		isWaitingForResponse = true;
	}

	public HLayout loginForm() {

		final RestDataSource dataSource = new RestDataSource();
		dataSource.setDataURL("http://localhost:8080/healthCard/usermanagement/login");
		dataSource.setRecordXPath("LoginOutput/user");
		dataSource.setCallbackParam("LoginOutput");
		dataSource.setDataFormat(DSDataFormat.JSON);

		dataSource.addHandleErrorHandler(new HandleErrorHandler() {

			public void onHandleError(ErrorEvent event) {
				event.cancel();
				authentication.setLogged(false);
				SC.say("Unauthorized! Please enter right credentials.");
			}
		});

		DataSourceTextField numberField = new DataSourceTextField(200, 30, "number", "N.O.", null, true);
		DataSourcePasswordField passwordField = new DataSourcePasswordField(200, 30, "password", "Password", null, true);
		dataSource.setFields(numberField, passwordField);

		final DynamicForm form = new DynamicForm();
		form.setDataSource(dataSource);
		form.setUseAllDataSourceFields(true);

		final DSCallback dsCallback = new DSCallback() {
			public void execute(DSResponse response, Object rawData, DSRequest request) {
				if (form.getValueAsString("number").equals(response.getData()[0].getAttributeAsString("number"))) {
					authentication.setLogged(true);
				}
			}
		};

		CrossMainViewButton loginButton = new CrossMainViewButton("Log in", new DoctorView.Factory(), new ClickHandler() {
			public void onClick(final ClickEvent event) {
				if (form.validate()) {
					form.saveData(dsCallback);
					loginSuccess(event);
				}
			}
		});

		loginButton.setHeight("30px");

		form.setStyleName("loginForm");

		HLayout healthCardLabel = componentsFactory.createHLayoutWithText("healthCard", null, 70, Alignment.CENTER);
		healthCardLabel.setStyleName("loginLabel");
		HLayout onlineHealthLabel = componentsFactory.createHLayoutWithText("your online health...", null, 30, Alignment.CENTER);
		onlineHealthLabel.setStyleName("onlineHealthLabel");
		HLayout loginLayout = componentsFactory.createHLayoutWithText(null, null, 70, Alignment.CENTER);
		loginLayout.addMember(form);
		HLayout buttonLayout = componentsFactory.createHLayoutWithText(null, null, 40, Alignment.CENTER);
		buttonLayout.setPadding(10);
		buttonLayout.addMember(loginButton);

		VLayout mainLayout = new VLayout();
		mainLayout.addMember(healthCardLabel);
		mainLayout.addMember(onlineHealthLabel);
		mainLayout.addMember(loginLayout);
		mainLayout.addMember(buttonLayout);
		mainLayout.setAlign(Alignment.CENTER);
		mainLayout.setAlign(VerticalAlignment.CENTER);
		this.addMember(mainLayout);
		return this;
	}

	private void loginSuccess(ClickEvent event) {
		if (authentication.isLogged()) {
			new ApplicationClickHandler().onClick(event);
		}
	}

	public static class Factory implements LayoutFactory {

		public Canvas create() {
			return new LoginView();
		}

	}

}
